package com.example.s05_shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.s05_shop.pojo.Category;
import com.example.s05_shop.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@Controller
@RequestMapping("/admin/category")
public class CategoryController {


    @Autowired
    private ICategoryService categoryService;

    @RequestMapping("/list")
    public String list(@RequestParam(value = "page",required = true,defaultValue = "1") int page,
                       @RequestParam(value = "size",required = true,defaultValue = "2") int size,
                       Model m)
    {
        System.out.println("catgory list()");
        // 查询商品分类
//        List<Category> list = categoryService.list();
        // 页面信息
        IPage<Category> pageBean = new Page<>();
        pageBean.setCurrent(page);// 第几页
        pageBean.setSize(size);// 每页数量

        // 查询
        IPage<Category> pageInfo = categoryService.page(pageBean);

        // 放入model
//        m.addAttribute("list",list);
        m.addAttribute("pageBean",pageBean);

        // 返回视图
        return "admin/category/list";
    }

    public static final String UPLOADED_FOLDER = "./upload/";


    @RequestMapping("/add")
    public String add(Category c,@RequestParam("file") MultipartFile file)
    {
        byte[] bytes = new byte[0];
        try {
            bytes = file.getBytes();
            // UPLOADED_FOLDER 文件本地存储地址
            Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
            Files.write(path, bytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        c.setPic("/images/"+file.getOriginalFilename());

        // 调用service保存商品分类
        boolean result = categoryService.save(c);

        // 跳转商品列表页
        return "redirect:/admin/category/list";
    }

    @RequestMapping("/del")
    public String del(int id)
    {
        // 调用service保存商品分类
        boolean result = categoryService.removeById(id);
        // 跳转商品列表页
        return "redirect:/admin/category/list";
    }


    @RequestMapping("/find")
    @ResponseBody
    public Category find(int id)
    {
        // 查询商品分类
        Category c = categoryService.getById(id);
        // 返回商品的json数据
        return c;
    }

    @RequestMapping("/modify")
    public String modify(Category c)
    {
        // 调用service保存商品分类
        boolean result = categoryService.updateById(c);
        // 跳转商品列表页
        return "redirect:/admin/category/list";
    }
}
